﻿using System;
using System.Collections.Generic;
using System.Linq;
using AMM.Business.Project;
using AMM.Business.Store;
using AMM.Model;
using AMM.Model.Project;
using AMM.Model.Store;

namespace AMM.UserControls {
    public partial class OutsourceQuotationSearchControl : BaseSearchControl {

        public override BaseSearchModel GetSearchModel() {
            int vendorValue = Convert.ToInt32(this.ddlVendor.SelectedValueEx);
            int materialValue = Convert.ToInt32(this.ddlProduct.SelectedValueEx);
            int priceFrom, priceTo;
            int.TryParse(this.tbPriceStart.Text, out priceFrom);
            int.TryParse(this.tbPriceEnd.Text, out priceTo);

            OutsourceQuotationSearchModel searchResult = new OutsourceQuotationSearchModel();
            searchResult.PriceStart = priceFrom;
            searchResult.PriceEnd = priceTo;
            searchResult.VendorId = vendorValue;
            searchResult.ProductId = materialValue;

            if (vendorValue == 0) { searchResult.VendorId = null; }
            if (materialValue == 0) { searchResult.ProductId = null; }
            if (string.IsNullOrEmpty(this.tbPriceStart.Text)) { searchResult.PriceStart = null; }
            if (string.IsNullOrEmpty(this.tbPriceEnd.Text)) { searchResult.PriceEnd = null; }

            return searchResult;
        }

        public override void ResetSearchModel() {
            this.tbPriceStart.Clear();
            this.tbPriceEnd.Clear();
            this.ddlVendor.SelectedValueEx = null;
            this.ddlProduct.SelectedValueEx = null;
        }

        public OutsourceQuotationSearchControl() {
            this.InitializeComponent();            
        }

        private void BindVendor() {
            VendorSearchModel searchModel = new VendorSearchModel() { IsEnabled = true };
            VendorProcessor vendorProcessor = new VendorProcessor(int.MaxValue);
            List<VendorViewModel> vendorList = vendorProcessor.FetchVendorViewModelList(searchModel).ToList();

            this.ddlVendor.DisplayMemberEx = "Code";
            this.ddlVendor.ValueMemberEx = "Key";
            this.ddlVendor.HeightEx = 300;
            this.ddlVendor.WidthEx = 400;
            this.ddlVendor.PageSizeEx = 20;
            this.ddlVendor.DataBind(new VendorViewModel(), x => new { x.Code, x.Name, x.Address }, vendorList);
        }

        private void BindProduct() {
            ProductSearchModel searchModel = new ProductSearchModel() { IsEnabled = true };
            ProductProcessor productProcessor = new ProductProcessor(int.MaxValue);
            List<ProductViewModel> productList = productProcessor.FetchProductViewModelList(searchModel).ToList();

            this.ddlProduct.DisplayMemberEx = "Code";
            this.ddlProduct.ValueMemberEx = "Key";
            this.ddlProduct.HeightEx = 300;
            this.ddlProduct.WidthEx = 400;
            this.ddlProduct.PageSizeEx = 20;
            this.ddlProduct.DataBind(new ProductViewModel(), x => new { x.Code, x.Name, x.ProductType }, productList);
        }

        private void OutsourceQuotationSearchControl_Load(object sender, EventArgs e) {
            this.BindVendor();
            this.BindProduct();
        }
    }
}
